import { SET_USERINFO, SET_ISAUTH } from "./../mutations_type"

import { dateDifference } from "_u/date"
import func from "_u/func"

const state = {
  userInfo: {},
  isAuth: false,
  hasUserInfo: false,
}

const mutations = {
  [SET_USERINFO](state, data) {
    state.userInfo = data
  },
  [SET_ISAUTH](state, data) {
    state.isAuth = data
  },
}

const getters = {
  userInfo: (state) => {
    return state.userInfo
  },
  userId: (state) => {
    return state.userInfo.userId
  },
  isAuth: (state) => {
    return state.isAuth
  },
  isVip: (state) => {
    return state.userInfo.isVip || false
  },
  isAdmin: (state) => {
    return state.userInfo.isAdmin || false
  },
}

const actions = {
  // 获取用户信息
  async getUserInfo({ commit }) {
    try {
      let user = null
      // user = await qq.BaaS.auth.loginWithQQ()
      user = await wx.BaaS.auth.loginWithWechat()

      const isVip = user.get("isVip")
      const isAdmin = user.get("isAdmin")
      // user 为 currentUser 对象
      commit(SET_USERINFO, {
        isVip,
        isAdmin,
        nickname: user.nickname,
        avatar: user.avatar,
        userId: user.user_id,
        created_at: user.created_at,
      })
      commit(SET_ISAUTH, user.is_authorized || false)
      return true
    } catch (err) {
      console.log("getUserInfo", err)
      commit(SET_ISAUTH, false)
      return false
    }
  },

  // 微信用户授权
  async authUserInfo({ commit }, userInfo) {
    uni.showLoading({ title: "登录中..." })

    let res = null
    //#ifdef MP-QQ
    res = await qq.BaaS.auth.loginWithQQ(userInfo)
    //#endif
    //#ifdef MP-WEIXIN
    res = await wx.BaaS.auth.loginWithWechat(userInfo)
    //#endif

    uni.hideLoading()
    commit(SET_USERINFO, {
      nickname: res.nickname,
      avatar: res.avatar,
      userId: res.user_id,
      created_at: res.created_at,
    })
    commit(SET_ISAUTH, true)
    return true
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions,
}
